﻿#include"ProductModel.h"

ProductModel::ProductModel()
	:XQDatabaseData()
{
}

ProductModel::ProductModel(const QVariantMap& data, bool base64)
{
	setData(data,base64);
}

ProductModel::~ProductModel()
{
}

QStringList ProductModel::fields() const
{
	return {"product_id","name","type","price","number","mtime" ,"ctime"
	};
}

QVariantMap ProductModel::toMap(const QStringList& fields, bool base64) const
{
	auto comparison = this->ProductModel::fields();
	int nSel = 0;
	QVariantMap map;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_product_id;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_name;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_type;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_price;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_number;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_mtime;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_ctime;
	return map;
}

void ProductModel::setData(const QVariantMap& data, bool base64)
{
	auto comparison = this->ProductModel::fields();
	int nSel = 0;
	if (data.contains(comparison[nSel]))
		m_product_id = data[comparison[nSel++]].toULongLong();
	if (data.contains(comparison[nSel]))
		m_name = data[comparison[nSel++]].toString();
	if (data.contains(comparison[nSel]))
		m_type = data[comparison[nSel++]].toString();
	if (data.contains(comparison[nSel]))
		m_price = data[comparison[nSel++]].toDouble();
	if (data.contains(comparison[nSel]))
		m_number = data[comparison[nSel++]].toULongLong();
	if (data.contains(comparison[nSel]))
		m_mtime = data[comparison[nSel++]].toDateTime();
	if (data.contains(comparison[nSel]))
		m_ctime = data[comparison[nSel++]].toDateTime();
}

QString ProductModel::createTableSql(const QString& tableName) const
{
	return QString("CREATE TABLE `%1` ("
		"product_id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"//id
		"name VARCHAR(50),"//名字
		"type VARCHAR(50),"//类型
		"price DOUBLE ,"//价格
		"number BIGINT UNSIGNED,"//数量
		"mtime DATETIME ,"//修改时间
		"ctime DATETIME "//创建时间
		")").arg(tableName);
}

quint64 ProductModel::product_id() const
{
	return m_product_id;
}

QString ProductModel::name() const
{
	return m_name;
}

QString ProductModel::type() const
{
	return m_type;
}

double ProductModel::price() const
{
	return m_price;
}

quint64 ProductModel::number() const
{
	return m_number;
}

QDateTime ProductModel::ctime() const
{
	return m_ctime;
}

QDateTime ProductModel::mtime() const
{
	return m_mtime;
}

double ProductModel::totalPrice() const
{
	return price()*number();
}

void ProductModel::setProduct_id(quint64 product_id)
{
	m_product_id = product_id;
	setMTime();
}

void ProductModel::setName(const QString& name)
{
	m_name = name;
	setMTime();
}

void ProductModel::setType(const QString& type)
{
	m_type = type;
	setMTime();
}

void ProductModel::setPrice(double price)
{
	m_price = price;
	setMTime();
}

void ProductModel::setNumber(quint64 number)
{
	m_number = number;
	setMTime();
}

void ProductModel::setCTime(const QDateTime& time)
{
	m_ctime = time;
}

void ProductModel::setMTime(const QDateTime& time)
{
	m_mtime = time;
}
